package com.soen341.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

//import com.soen341.model.Course;
import com.soen341.model.CourseSequence;
import com.soen341.model.SequenceMember;

//--------------------------------------------------------------------------------------------------------------------------------
/**
* Sequence DAO concrete implementation.
*/
//--------------------------------------------------------------------------------------------------------------------------------
@Repository("sequenceDao")
public class SequenceDao
{
	private EntityManager entityManager;

	// -------------------------------------------------------------------------------
	@PersistenceContext
	public void setEntityManager(EntityManager entityManager)
	{
		this.entityManager = entityManager;
	}

	// -------------------------------------------------------------------------------
	public EntityManager getEntityManager()
	{
		return entityManager;
	}
	
	@SuppressWarnings("unchecked")
	public List<SequenceMember> loadSequenceMembers(String sequenceId)
	{	
		Query queryLoadSequence = entityManager.createNamedQuery("getSequence");
		queryLoadSequence.setParameter("sequenceId", sequenceId);

		List<SequenceMember> sequenceMembers = queryLoadSequence.getResultList(); 

		return sequenceMembers;
	}

	// -------------------------------------------------------------------------------
	@SuppressWarnings("unchecked")
	public CourseSequence loadSequenceObject(String sequenceId)
	{
		Query queryLoadSequence = entityManager.createNamedQuery("getSequenceById");
		queryLoadSequence.setParameter("sequenceId", sequenceId);

		List<CourseSequence> sequences = queryLoadSequence.getResultList(); 

		return sequences.get(0);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String,Object>> loadSequenceMembersBySemester(String sequenceId)
	{			
		List<Map<String,Object>> sequence = new ArrayList<Map<String,Object>>();
	
		Query queryLoadSequenceForYears = entityManager.createNamedQuery("getSequence");
		queryLoadSequenceForYears.setParameter("sequenceId", sequenceId);
		
		int numberOfYears = 0;
		
		List<SequenceMember> sequenceMembersForYear = queryLoadSequenceForYears.getResultList(); 
		
		for (int y=0; y < sequenceMembersForYear.size(); ++y){
		
			if(sequenceMembersForYear.get(y).getCourseYear() > numberOfYears){
				numberOfYears = sequenceMembersForYear.get(y).getCourseYear();
			}
			
		}
			//get parameters from course using the course id
		Query queryLoadSequence = entityManager.createNamedQuery("getSequenceBySemester");
		queryLoadSequence.setParameter("sequenceId", sequenceId);
		
		for (int i = 1; i < numberOfYears ; ++i){
			
			//1=summer 2=fall 3=fall/winter 4=winter so will never be 5
			for (int semesterID = 1; semesterID < 5; ++semesterID){

				if (semesterID == 3){
					continue;
				}
				
				queryLoadSequence.setParameter("semester", semesterID);		
				queryLoadSequence.setParameter("courseYear", i);
			
				if (semesterID == 1){
					queryLoadSequence.setParameter("twoSemesters", null);		
				}
			
				else {
					queryLoadSequence.setParameter("twoSemesters", 3);		
				}
			
				List<SequenceMember> sequenceMembers = queryLoadSequence.getResultList(); 
			
				Map<String,Object> map = new HashMap<String,Object>();
		
				String semesterSTR;
			
				switch(semesterID){
					case 1:
						semesterSTR = "Summer";
						break;
					case 2:
						semesterSTR = "Fall";
						break;
					case 3:
						semesterSTR = "Fall/Winter";
						break;
					case 4:
						semesterSTR = "Winter";
						break;
					default:
						semesterSTR = "";
						break;			
				}
			
				if (sequenceMembers.size() == 0 && semesterID == 1){
					//there is no summer semester do not add it
				}
				
				else {
					
					/*Query queryLoadCourses = entityManager.createNamedQuery("getCourse");
					List<Course> courseList = new ArrayList<Course>(); 

					for (int w = 0; w < sequenceMembers.size(); ++w){
						queryLoadSequence.setParameter("twoSemesters", null);
						
						Course course = (CoursequeryLoadCourses.getSingleResult();
						courseList.add(course);
					}*/
					
					map.put("courses", sequenceMembers);
					map.put("semester", semesterSTR + " Year " + i );
					sequence.add(map);	
				}
			}
		}
		
		return sequence;
	}
}
